package glen.mgbean;

import glen.security.dto.User;
import glen.security.service.UserSvr;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.List;

public class UserMgb implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -2658816198129548602L;
    private Integer userIdSearch;
    private String userNameSearch;
    private Integer activeSearch;
    private List<User> resultSearch;
    private User selected;
    private DataTable dtResult;
    UserSvr dao = new UserSvr();

    public UserMgb() {
        super();
        this.reset();
    }

    public DataTable getDtResult() {
        return dtResult;
    }

    public void setDtResult(DataTable dtResult) {
        this.dtResult = dtResult;
    }

    @Override
    public String toString() {
        return "UserMgb [userIdSearch=" + userIdSearch + ", userNameSearch="
                + userNameSearch + ", activeSearch=" + activeSearch
                + ", resultSearch=" + resultSearch + ", selected=" + selected
                + "]";
    }

    public Integer getUserIdSearch() {
        return userIdSearch;
    }

    public void setUserIdSearch(Integer userIdSearch) {
        this.userIdSearch = userIdSearch;
    }

    public String getUserNameSearch() {
        return userNameSearch;
    }

    public void setUserNameSearch(String userNameSearch) {
        this.userNameSearch = userNameSearch;
    }

    public Integer getActiveSearch() {
        return activeSearch;
    }

    public void setActiveSearch(Integer activeSearch) {
        this.activeSearch = activeSearch;
    }

    public List<User> getResultSearch() {
        return resultSearch;
    }

    public void setResultSearch(List<User> resultSearch) {
        this.resultSearch = resultSearch;
    }

    public User getSelected() {
        return selected;
    }

    public void setSelected(User selected) {
        this.selected = selected;
    }

    public void add() {
        selected = new User();
    }

    public void reset() {
        userIdSearch = null;
        userNameSearch = null;
        selected = null;
    }

    public void search() {

        User o = new User();
        o.setUserId(userIdSearch);
        o.setUserName(userNameSearch);
        resultSearch = dao.search(o);
    }

    public void view(User user) {
        selected = user;
    }

    public boolean checkDup(String value) {
        User item = new User();
        item.setUserName(value);
        if (dao.search(item).size() == 0)
            return false;
        return true;
    }

    public void save() {
        FacesContext context = FacesContext.getCurrentInstance();

        if (selected.getUserId() != null)
            dao.update(selected);
        else {
            if (checkDup(selected.getUserName())) {
                RequestContext.getCurrentInstance().addCallbackParam(
                        "checkBusinessFail", true);
                context.addMessage("growl", new FacesMessage(
                        FacesMessage.SEVERITY_ERROR, "Add Error",
                        "Duplicate Name:" + selected.getUserName()));
                return;
            }
            dao.create(selected);
        }
        context.addMessage("growl", new FacesMessage("Operation Success",
                "Save ID: " + selected.getUserId()));
        search();

    }

    public void delete() {

        dao.delete(selected.getUserId());
        search();
        FacesContext.getCurrentInstance().addMessage(
                "growl",
                new FacesMessage("Operation Success", "Delete ID: "
                        + selected.getUserId())
        );
    }
}
